<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2019/1/28
 * Time: 09:50
 */

namespace app\modules\repository;


class MemoryStorage
{
    private $_data = [];

    private $_lastId = 0;

    public function persist(array $data): int
    {
        $this->_lastId++;

        $data['id'] = $this->_lastId;
        $this->_data[$this->_lastId] = $data;

        return $this->_lastId;
    }

    public function retrieve(int $id): array
    {
        if (!isset($this->_data[$id])) {
            throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
        }

        return $this->_data[$id];
    }

    public function delete(int $id)
    {
        if (!isset($this->_data[$id])) {
            throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
        }

        unset($this->_data[$id]);
    }
}